package com.example.user.controller;

import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.enums.UserIdentity;
import com.example.user.aspect.CheckUserIdentity;
import com.example.user.domain.video.dto.VideoQueryDTO;
import com.example.user.domain.video.dto.VideoReNameDTO;
import com.example.user.domain.video.vo.VideoVO;
import com.example.user.service.IVideoService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/video")
public class VideoController extends BaseController {

    @Autowired
    private IVideoService videoService;

    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo getVideoList(VideoQueryDTO videoQueryDTO) {
        return getTableDataInfo(videoService.list(videoQueryDTO));
    }

    @PostMapping("/upload")
    @CheckUserIdentity(identity = UserIdentity.TEACHER)
    public ResponseEntity<R<String>> uploadVideo(
            @RequestParam("file") MultipartFile videoFile,  // 匹配前端参数名
            @RequestParam("courseId") Long courseId) {
        return videoService.upload(videoFile,courseId);
    }

    @DeleteMapping("/delete")
    @CheckUserIdentity(identity = UserIdentity.TEACHER)
    public R<Void> delete(Long videoId) {
        return toR(videoService.delete(videoId));
    }


    @PutMapping("/reName")
    @CheckUserIdentity(identity = UserIdentity.TEACHER)
    public R<Void> reName(@RequestBody VideoReNameDTO reNameDTO) {
        return toR(videoService.reName(reNameDTO.getVideoId(), reNameDTO.getNewName()));
    }

    @GetMapping("/get")
    public R<VideoVO> get(Long videoId) {
        return R.ok(videoService.get(videoId));
    }

    @GetMapping("/video-stream")
    public R<Void> streamVideo(@RequestParam("videoId") Long videoId, HttpServletRequest httpServletRequest, HttpServletResponse response) {
        videoService.streamVideo(videoId,response);
        return R.ok();
    }

}
